//server
//
//Ky Halverson

#include"masterSocket.cpp"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <netinet/in.h>

#define PORT 9000
#define TYPE "TCP"


using namespace std;

bool requestCheckGet(string message);
bool requestCheckVersion(string message);
string requestMessage(string message);

int main()
{
	masterSocket* serverSocket;
	serverSocket = new masterSocket(PORT, TYPE);

	string message;
	string request;
	string file;
	string temp;	

	char* fileData;

	bool requestGET = true;
	bool requestVersion = true;
	
	int header;
	int fileLength;
	int lengthTemp;
	int startTemp;
	

	//load file and prep for sending
  	ifstream myFile("index.html",ifstream::out);
	
	myFile.seekg(0,ios::end);
	fileLength = myFile.tellg();
	
	myFile.seekg(0,ios::beg); 
	fileData = new char[fileLength]; 

	myFile.read(fileData,fileLength); 
	
	file = fileData;
		

	 
	
 while(true)
 {
			//get and check request
			message = serverSocket->recvIt("SERVER");
			header = message.find_first_of("\n");
cout<<header<<endl;
			message = message.substr(0,header+1);
cout<<message<<endl;
			if (message == "GET /EXIT HTTP/1.1\r\n")
			{
				exit(EXIT_SUCCESS);
			}

			std::cout<<"Request recieved: "<<std::endl;
			std::cout<<message<<std::endl;
			
			

			requestGET = requestCheckGet(message);
			requestVersion = requestCheckVersion(message);
			
			//check to see request is standard GET
			if((requestGET == true) && (requestVersion == true))
			{

			 request = requestMessage(message);
			 
			 if ((request == "/index.html")||(request =="/"))
			 {
				
				
			 	if (fileLength < 6000)
			 	{
			 		cout<<"sending"<<endl;
					serverSocket->sendIt("SERVER", "HTTP/1.1 200 OK\r\n");
					serverSocket->sendIt("SERVER", "\r\n");
					serverSocket->sendIt("SERVER",file);
					cout<<"sent"<<endl;
				}
				else
				{
					cout<<"sending"<<endl;
					serverSocket->sendIt("SERVER", "HTTP/1.1 200 OK\r\n");
					serverSocket->sendIt("SERVER", "\r\n");
					startTemp = 0;
					lengthTemp = 5999;
					while (lengthTemp <= fileLength)
					{
						temp = file.substr(startTemp,lengthTemp);
						serverSocket->sendIt("SERVER",temp);
						if(5999+lengthTemp < fileLength)
						{
							startTemp = lengthTemp+1;
							lengthTemp = lengthTemp+5999;
						}
						else
						{
							startTemp = lengthTemp+1;
							lengthTemp=fileLength;
						}						
					}
					cout<<"sent"<<endl;
				}
				if (request =="/"){break;}
				
			 }
			 else
			 {
				serverSocket->sendIt("SERVER","HTTP/1.1 404 Not found\r\n");
			 }
			}
			else
			{
				serverSocket->sendIt("SERVER","HTTP/1.1 400 Bad request\r\n");
			}
			




 }


return 0; 
}


bool requestCheckGet(string checkMessage)
{
	string message = checkMessage;
	string check;
	check = message.substr(0,3);
	if (check == "GET")
	{
		return true;
	}
	else
	{
		return false;
	}
	
	
}

bool requestCheckVersion(string checkMessage)
{
	string message = checkMessage;
	int size = message.length();
	string check = message.substr(size-10,size);
	if (check == "HTTP/1.1\r\n")
	{	
		return true;
	}
	else
	{
		return false;
	}
	
     
}

string requestMessage(string checkMessage)
{
	
	string message = checkMessage;
	int size = message.length();
	string check = message.substr(4,size-15);
	cout<<"Client Asked for File:"<<check<<":end"<<endl;
	return check;
}
